

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.ico">
  <link rel="icon" href="/img/favicon.ico">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="将一些C语言的易遗忘或之前未学习过的知识点再次记录">
  <meta name="author" content="叶尘">
  <meta name="keywords" content="">
  
  <title>考研复试-C语言学习 - 叶尘的技术升级小木屋</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.7.2/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->

  
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/shubiao.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/gundongtiao.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/daziyanse.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/toubudaziji.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"www.yechen-blog.cn","root":"/","version":"1.8.11","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>YECHEN</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-qrcode"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/playlist/">
                <i class="iconfont icon-music"></i>
                音乐
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" target="_self" role="button"
                 data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="iconfont icon-books"></i>
                链接
              </a>
              <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/">
                    
                    主题博客
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/guide/">
                    
                    配置指南
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/icon/">
                    
                    图标用法
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://www.bilibili.com">
                    
                    哔哩哔哩
                  </a>
                
              </div>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/banner.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="考研复试-C语言学习">
              
            </span>

            
              <div class="mt-3">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-author" aria-hidden="true"></i>
      叶尘
    </span>
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2023-03-06 14:55" pubdate>
        星期一, 三月 6日 2023, 2:55 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      3.5k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      45
       分钟
    </span>
  

  
  
    
      <!-- LeanCloud 统计文章PV -->
      <span id="leancloud-page-views-container" class="post-meta" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="leancloud-page-views"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">考研复试-C语言学习</h1>
            
            <div class="markdown-body">
              <h2 id="C-语言运行机制（过程）简述"><a href="#C-语言运行机制（过程）简述" class="headerlink" title="C 语言运行机制（过程）简述"></a>C 语言运行机制（过程）简述</h2><ol>
<li>编辑：编写 .c 文件，源文件</li>
<li>编译：将 .c 程序翻译成 目标文件(.obj)，在计算机底层执行</li>
<li>链接：将<strong>目标文件 .obj + 库文件</strong>链接成可执行文件(.exe)，在计算机底层执行</li>
<li>运行：执行 .exe 文件，得到运行结果</li>
</ol>
<h2 id="C语言执行程序流程图解"><a href="#C语言执行程序流程图解" class="headerlink" title="C语言执行程序流程图解"></a>C语言执行程序流程图解</h2><p><img src="https://yechen-blog-image.oss-cn-hangzhou.aliyuncs.com/images/image-20230225113011156.png" srcset="/img/loading.gif" lazyload alt="image-20230225113011156"></p>
<h2 id="C语言转义字符"><a href="#C语言转义字符" class="headerlink" title="C语言转义字符"></a>C语言转义字符</h2><ol>
<li><p><code>\t</code>：一个制表位，实现对齐功能</p>
</li>
<li><p><code>\n</code>：换行</p>
</li>
<li><p><code>\\</code>：输出一个 <code>\</code></p>
</li>
<li><p><code>\&quot;</code>：输出一个 <code>”</code></p>
</li>
<li><p><code>\&#39;</code>：输出一个 <code>’</code></p>
</li>
<li><p><code>\r</code>：一个回车 </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;1234567\r99&quot;</span>);<span class="hljs-comment">// 输出：9934567</span><br></code></pre></td></tr></table></figure></li>
</ol>
<h2 id="变量的数据类型"><a href="#变量的数据类型" class="headerlink" title="变量的数据类型"></a>变量的数据类型</h2><p><img src="https://yechen-blog-image.oss-cn-hangzhou.aliyuncs.com/images/image-20230225134301276.png" srcset="/img/loading.gif" lazyload alt="image-20230225134301276"></p>
<ul>
<li><p>在 C 语言中，没有字符串类型，是使用字符数组表示的</p>
</li>
<li><p>在不同系统上，部分数据类型的字节长度不一样，如 int 可能有 2 字节或 4 字节</p>
</li>
</ul>
<h2 id="浮点型数据"><a href="#浮点型数据" class="headerlink" title="浮点型数据"></a>浮点型数据</h2><p>浮点型常量默认是 double 类型，声明 float 类型常量时，须在后面加 f 或 F</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-keyword">double</span> a = <span class="hljs-number">5.12</span>;<br><span class="hljs-keyword">double</span> b = <span class="hljs-number">.512</span>;<span class="hljs-comment">// 等于 0.512</span><br><span class="hljs-keyword">double</span> c = <span class="hljs-number">5.12e2</span>;<span class="hljs-comment">// 等于 5.12*10^2</span><br><span class="hljs-keyword">double</span> d = <span class="hljs-number">5.12e-2</span>;<span class="hljs-comment">// 等于 5.12*10^(-2)</span><br><span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%f\n&quot;</span>, a);<span class="hljs-comment">// %f 默认精度为小数点后 6 位</span><br><span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%f\n&quot;</span>, b);<br><span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%f\n&quot;</span>, c);<br><span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%f\n&quot;</span>, d);<br><span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%.15f\n&quot;</span>, a);<br><span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%.15f\n&quot;</span>, b);<br><span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%.15f\n&quot;</span>, c);<br><span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%.15f\n&quot;</span>, d);<br><span class="hljs-comment">/*</span><br><span class="hljs-comment"></span><br><span class="hljs-comment">输出：</span><br><span class="hljs-comment">5.120000</span><br><span class="hljs-comment">0.512000</span><br><span class="hljs-comment">512.000000</span><br><span class="hljs-comment">0.051200</span><br><span class="hljs-comment">5.120000000000000</span><br><span class="hljs-comment">0.512000000000000</span><br><span class="hljs-comment">512.000000000000000</span><br><span class="hljs-comment">0.051200000000000</span><br><span class="hljs-comment"></span><br><span class="hljs-comment">*/</span><br></code></pre></td></tr></table></figure>

<h2 id="指针（初级）"><a href="#指针（初级）" class="headerlink" title="指针（初级）"></a>指针（初级）</h2><p>简单的说，指针表示的就是一个地址（指针变量中存放的值是一个地址）</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-comment">// 输出变量的地址</span><br><span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>;<br><span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%p&quot;</span>, &amp;a);<br></code></pre></td></tr></table></figure>

<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-comment">// int * 定义变量为指针类型，变量名为 pInt，该变量中保存了变量 a 在内存中的地址</span><br><span class="hljs-keyword">int</span> *pInt = &amp;a;<br><span class="hljs-comment">// 当然 pInt 变量也有自身地址</span><br><span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%p\n&quot;</span>, &amp;pInt);<br></code></pre></td></tr></table></figure>

<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-comment">// 取出指针变量指向地址中保存的值</span><br><span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d\n&quot;</span>, *pInt);<br></code></pre></td></tr></table></figure>

<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-comment">// 通过指针修改 a 的值</span><br>*pInt = <span class="hljs-number">12</span>;<br><span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d\n&quot;</span>, a);<br></code></pre></td></tr></table></figure>



<h2 id="值传递与地址传递"><a href="#值传递与地址传递" class="headerlink" title="值传递与地址传递"></a>值传递与地址传递</h2><ul>
<li><p>值传递：将变量指向的存储内容，在传递/赋值时，拷贝一份给接收变量</p>
</li>
<li><p><strong>默认传递值的类型</strong>：基本数据类型 (整型类型、小数类型，字符类型), 结构体, 共用体</p>
<p><img src="https://yechen-blog-image.oss-cn-hangzhou.aliyuncs.com/images/image-20230225150107013.png" srcset="/img/loading.gif" lazyload></p>
</li>
<li><p>地址传递：也叫 指针传递，如果是指针，就将指针变量存储的地址，传递给接收变量，如果是数组，就将数组的首地址传递给接收变量</p>
</li>
<li><p><strong>默认传递地址的类似</strong>：指针、数组</p>
<p><img src="https://yechen-blog-image.oss-cn-hangzhou.aliyuncs.com/images/image-20230225150145083.png" srcset="/img/loading.gif" lazyload alt="image-20230225150145083"></p>
</li>
</ul>
<h2 id="常量"><a href="#常量" class="headerlink" title="常量"></a>常量</h2><p>定义后不能再修改</p>
<p>使用 <code>#define</code> 预处理器</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-comment">// #define 常量名 常量值（后面不加分号）</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> PAI 3.141592</span><br><span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%f&quot;</span>, PAI);<br></code></pre></td></tr></table></figure>

<p>使用 <code>const</code> 关键字</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-comment">// const 数据类型 常量名 = 常量值;（后面加分号）</span><br><span class="hljs-keyword">const</span> <span class="hljs-keyword">double</span> PAI = <span class="hljs-number">3.141592</span>;<br><span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%f&quot;</span>, PAI);<br></code></pre></td></tr></table></figure>

<p><code>#define</code> 与 <code>const</code> 的区别</p>
<ol>
<li><p>const 定义的常量时，带类型，define 不带类型</p>
</li>
<li><p>const 是在 编译、运行的时候起作用，而 define 是在编译的<strong>预处理阶段</strong>起作用</p>
</li>
<li><p>define 只是简单的替换，没有类型检查。简单的字符串替换会导致边界效应</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> A 1</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> B A+3</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> C A/B*3</span><br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d&quot;</span>, C);<span class="hljs-comment">// 运算表达式为：1/A+3*3=1+9=10</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br><br></code></pre></td></tr></table></figure></li>
<li><p>const 常量可以进行调试的，define 是不能进行调试的，主要是预编译阶段就已经替换掉了，调试的时候就没它了</p>
</li>
<li><p>const 不能重定义，不可以定义两个一样的，而 define 通过 undef 取消某个符号的定义，再重新定义</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> A 1</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">undef</span> A</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> A 3</span><br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d&quot;</span>, A);<span class="hljs-comment">// 输出 3</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure></li>
<li><p>define 可以配合#ifdef、 #ifndef、 #endif 来使用， 可以让代码更加灵活，比如我们可以通过#define 来 启动或者关闭 调试信息</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> DEBUG 1</span><br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br><span class="hljs-meta">#<span class="hljs-meta-keyword">ifdef</span> DEBUG</span><br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;DEBUG&quot;</span>);<br><span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">ifndef</span> DEBUG</span><br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;NO_DEBUG&quot;</span>);<br><span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure></li>
</ol>
<h2 id="include-lt-gt-与-include-“”-的区别"><a href="#include-lt-gt-与-include-“”-的区别" class="headerlink" title="include&lt;&gt; 与 #include “” 的区别"></a>include&lt;&gt; 与 #include “” 的区别</h2><p><code>#include &lt;&gt;</code> 引用的是编译器的类库路径下的头文件，用户引用系统头文件</p>
<p><code>#include &quot;&quot;</code> 引用的是自己的程序目录的相对路径中的头文件，如果没有找到，才回去编译器类的类库路径下找头文件</p>
<p>总结：</p>
<ul>
<li>对于系统头文件，两个方式都可以，但是 <code>#include &lt;&gt;</code> 方式效率会高些</li>
<li>对于自定义用户头文件，只可以使用 <code>#include &quot;&quot;</code> 方式引入</li>
</ul>
<h2 id="自定义头文件"><a href="#自定义头文件" class="headerlink" title="自定义头文件"></a>自定义头文件</h2><p><code>MyFun.h</code></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-comment">// 只能声明，不能实现</span><br><span class="hljs-comment">// 可以包含函数声明和宏定义</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">sayHello</span><span class="hljs-params">()</span></span>;<br></code></pre></td></tr></table></figure>

<p><code>MyFun.c</code></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&quot;MyFun.h&quot;</span><span class="hljs-comment">// 引入自定义头文件</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cstdio&gt;</span></span><br><br><span class="hljs-comment">// 实现方法</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">sayHello</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Hello&quot;</span>);<br>&#125;<br></code></pre></td></tr></table></figure>

<p><code>main.c</code></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&quot;MyFun.h&quot;</span><span class="hljs-comment">// 引入自定义头文件</span></span><br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-comment">// 调用自定义头文件中的方法</span><br>    sayHello();<br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>个人理解</strong></p>
<blockquote>
<p>类比 Java，<code>.h</code> 文件相当于 Java 中定义的接口，同名（当然也可以不同名）的 <code>.c</code> 文件就是对于该接口的实现。最后在主函数中引入接口，面向接口编程。</p>
</blockquote>
<h2 id="变量初始化问题"><a href="#变量初始化问题" class="headerlink" title="变量初始化问题"></a>变量初始化问题</h2><ul>
<li><p>局部变量：系统不会对其进行默认初始化，必须手动初始化</p>
</li>
<li><p>全局变量：系统会对其进行默认初始化，默认值如下：</p>
<p><img src="https://yechen-blog-image.oss-cn-hangzhou.aliyuncs.com/images/image-20230227124612950.png" srcset="/img/loading.gif" lazyload alt="image-20230227124612950"></p>
</li>
</ul>
<h2 id="C-程序的内存分布图"><a href="#C-程序的内存分布图" class="headerlink" title="C 程序的内存分布图"></a>C 程序的内存分布图</h2><ul>
<li>栈区：存放局部变量</li>
<li>堆区：存放动态分配（malloc）的数据</li>
<li>静态存储区：存放全局变量和静态数据</li>
<li>代码区：存放代码、指令</li>
</ul>
<p><img src="https://yechen-blog-image.oss-cn-hangzhou.aliyuncs.com/images/image-20230227125227077.png" srcset="/img/loading.gif" lazyload alt="image-20230227125227077"></p>
<p><img src="https://yechen-blog-image.oss-cn-hangzhou.aliyuncs.com/images/image-20230227143048500.png" srcset="/img/loading.gif" lazyload alt="image-20230227143048500"></p>
<p><img src="https://yechen-blog-image.oss-cn-hangzhou.aliyuncs.com/images/image-20230227143105599.png" srcset="/img/loading.gif" lazyload alt="image-20230227143105599"></p>
<h2 id="static-关键字"><a href="#static-关键字" class="headerlink" title="static 关键字"></a>static 关键字</h2><ul>
<li><p><code>static</code> 修饰<strong>局部变量</strong>：该变量在运行过程中保存在静态存储区，只声明一次，如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">fun1</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;n = %d\n&quot;</span>, a);<br>    a++;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;n = %d\n&quot;</span>, a);<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br>    fun1();<br>    fun1();<br>    fun1();<br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br><span class="hljs-comment">/*</span><br><span class="hljs-comment">输出：</span><br><span class="hljs-comment">n = 10</span><br><span class="hljs-comment">n = 11</span><br><span class="hljs-comment">n = 11</span><br><span class="hljs-comment">n = 12</span><br><span class="hljs-comment">n = 12</span><br><span class="hljs-comment">n = 13</span><br><span class="hljs-comment">*/</span><br></code></pre></td></tr></table></figure></li>
<li><p><code>static</code> 修饰<strong>全局变量</strong></p>
<ul>
<li><p>普通的全局变量：在一个文件中声明的全局变量，可以在另一个文件中使用 <code>extern</code> 引入，当前工程中的文件均可以访问，故变量名不可重复</p>
<p><code>main.c</code></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-keyword">extern</span> <span class="hljs-keyword">int</span> myObj;<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">fun1</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> a = <span class="hljs-number">10</span>;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;n = %d\n&quot;</span>, a);<br>    a++;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;n = %d\n&quot;</span>, a);<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d&quot;</span>, myObj);<br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><code>MyFun.c</code></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-keyword">int</span> myObj = <span class="hljs-number">100</span>;<br></code></pre></td></tr></table></figure></li>
<li><p><code>static</code> 修改的全局变量，只在当前文件中有效，到其他文件中不能访问</p>
</li>
</ul>
</li>
<li><p><code>static</code> 修饰<strong>函数</strong>：同全局变量</p>
</li>
</ul>
<h2 id="时间函数"><a href="#时间函数" class="headerlink" title="时间函数"></a>时间函数</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cstdio&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;ctime&gt;</span></span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">fun1</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> x = <span class="hljs-number">0</span>;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">1000000000</span>; ++i) &#123;<br>        x++;<br>    &#125;<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br><br>    <span class="hljs-keyword">time_t</span> currentTime1;<br>    <span class="hljs-comment">// 获取当前时间 1</span><br>    <span class="hljs-built_in">time</span>(&amp;currentTime1);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%s\n&quot;</span>, <span class="hljs-built_in">ctime</span>(&amp;currentTime1));<br>    <br>    <span class="hljs-comment">// 耗时函数</span><br>    <span class="hljs-built_in">fun1</span>();<br>    <br>    <span class="hljs-comment">// 获取当前时间 2</span><br>    <span class="hljs-keyword">time_t</span> currentTime2;<br>    <span class="hljs-built_in">time</span>(&amp;currentTime2);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%s\n&quot;</span>, <span class="hljs-built_in">ctime</span>(&amp;currentTime2));<br>    <br>    <span class="hljs-comment">// 计算两个时间差</span><br>    <span class="hljs-keyword">double</span> diff = <span class="hljs-built_in">difftime</span>(currentTime2, currentTime1);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%lf&quot;</span>, diff);<br><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="数据格式转换"><a href="#数据格式转换" class="headerlink" title="数据格式转换"></a>数据格式转换</h2><ul>
<li><p>数字 -&gt; 字符串：使用 <code>sprintf</code> 函数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cstdio&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cstdlib&gt;</span></span><br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br><br>    <span class="hljs-keyword">int</span> a = <span class="hljs-number">2434</span>;<br>    <span class="hljs-keyword">int</span> b = <span class="hljs-number">356</span>;<br>    <span class="hljs-keyword">char</span> *str = (<span class="hljs-keyword">char</span> *)(<span class="hljs-built_in">malloc</span>(<span class="hljs-built_in"><span class="hljs-keyword">sizeof</span></span>(<span class="hljs-keyword">char</span>) * <span class="hljs-number">20</span>));<br>    <span class="hljs-comment">// 数据转换</span><br>    <span class="hljs-built_in">sprintf</span>(str, <span class="hljs-string">&quot;%d + %d&quot;</span>, a, b);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%s\n&quot;</span>, str);<span class="hljs-comment">// 输出：2434 + 356</span><br>    <br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure></li>
<li><p>字符串 -&gt; 数字：方法位于 <code>stdlib.h</code> 中，如果格式不正确，强制转换为 <code>0</code></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cstdio&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cstdlib&gt;</span></span><br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br><br>    <span class="hljs-keyword">char</span> str1[<span class="hljs-number">20</span>] = <span class="hljs-string">&quot;423132&quot;</span>;<br>    <span class="hljs-keyword">char</span> str2[<span class="hljs-number">20</span>] = <span class="hljs-string">&quot;3.1415&quot;</span>;<br>    <span class="hljs-comment">// 整型 转 字符串</span><br>    <span class="hljs-keyword">int</span> int1 = <span class="hljs-built_in">atoi</span>(str1);<span class="hljs-comment">// 方法位于 s tdlib.h 包</span><br>    <span class="hljs-comment">// 浮点型 转 字符串</span><br>    <span class="hljs-keyword">double</span> double1 = <span class="hljs-built_in">atof</span>(str2);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d\n&quot;</span>, int1);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%lf\n&quot;</span>, double1);<br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure></li>
</ul>
<h2 id="预处理命令"><a href="#预处理命令" class="headerlink" title="预处理命令"></a>预处理命令</h2><ul>
<li>使用库函数之前，应该用 <code>#include</code> 引入对应的头文件。这种以 <code>#</code> 开头的命令称之为预处理命令</li>
<li>这些在编译之前对源文件进行简单加工的过程，称为预处理（机预先处理、提前处理）</li>
<li>预处理主要是处理以 <code>#</code> 开头的命令，例如 <code>#include &lt;stdio.h&gt;</code> 等。预处理命令要放在所有函数之外，而且，一般都放在源文件的前面</li>
<li>预处理是C语言的一个重要功能，由预处理程序完成。当对一个源文件进行编译时，系统将自动调佣预处理程序对源程序中的预处理部分做处理，处理完毕后自动进入对源程序的编译</li>
<li>C语言提供了多种预处理功能，如<strong>宏定义、文件包含、条件编译</strong>等，合理地使用它们会使编写的程序便于阅读、修改、移植和调试，也有助于模块化程序设计</li>
</ul>
<h2 id="多级指针"><a href="#多级指针" class="headerlink" title="多级指针"></a>多级指针</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cstdio&gt;</span></span><br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-keyword">int</span> value = <span class="hljs-number">10</span>;<br>    <span class="hljs-keyword">int</span>* pValue = &amp;value;<br>    <span class="hljs-keyword">int</span>** ppValue = &amp;pValue;<br>    <span class="hljs-keyword">int</span>*** pppValue = &amp;ppValue;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;&amp;pppValue: %p\n&quot;</span>, &amp;pppValue);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;pppValue: %p\n&quot;</span>, pppValue);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;&amp;ppValue: %p\n&quot;</span>, &amp;ppValue);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;ppValue: %p\n&quot;</span>, ppValue);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;&amp;pValue: %p\n&quot;</span>, &amp;pValue);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;pValue: %p\n&quot;</span>, pValue);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;&amp;Value: %p\n&quot;</span>, &amp;value);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Value: %d\n&quot;</span>, value);<br><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br><span class="hljs-comment">/*</span><br><span class="hljs-comment">输出：</span><br><span class="hljs-comment">&amp;pppValue: 000000a384dff680</span><br><span class="hljs-comment">pppValue: 000000a384dff688</span><br><span class="hljs-comment">&amp;ppValue: 000000a384dff688</span><br><span class="hljs-comment">ppValue: 000000a384dff690</span><br><span class="hljs-comment">&amp;pValue: 000000a384dff690</span><br><span class="hljs-comment">pValue: 000000a384dff69c</span><br><span class="hljs-comment">&amp;Value: 000000a384dff69c</span><br><span class="hljs-comment">Value: 10</span><br><span class="hljs-comment">*/</span><br></code></pre></td></tr></table></figure>

<h2 id="函数指针"><a href="#函数指针" class="headerlink" title="函数指针"></a>函数指针</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cstdio&gt;</span></span><br><br><span class="hljs-comment">// 定义一个普通的函数</span><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">add</span><span class="hljs-params">(<span class="hljs-keyword">int</span> a, <span class="hljs-keyword">int</span> b)</span> </span>&#123;<br>    <span class="hljs-keyword">return</span> a + b;<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 1. 函数指针的名字是 pAdd</span><br><span class="hljs-comment">     * 2. int 表示函数指针指向的函数的返回值类型是 int</span><br><span class="hljs-comment">     * 3. (int, int) 表示函数指针指向的函数的形式参数是两个 int 类型</span><br><span class="hljs-comment">     * 4. = add 表示函数指正指向 add 函数</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-built_in"><span class="hljs-keyword">int</span></span> (*pAdd)(<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>) = add;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 以下两种方法均可通过函数指针调用函数</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">int</span> sum1 = (*pAdd)(<span class="hljs-number">10</span>, <span class="hljs-number">100</span>);<br>    <span class="hljs-keyword">int</span> sum2 = <span class="hljs-built_in">pAdd</span>(<span class="hljs-number">10</span>, <span class="hljs-number">100</span>);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d\n&quot;</span>, sum1);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d\n&quot;</span>, sum2);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%p\n&quot;</span>, pAdd);<br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="回调函数"><a href="#回调函数" class="headerlink" title="回调函数"></a>回调函数</h2><blockquote>
<p>函数指针变量可以作为某个函数的一个参数来使用，回调函数就是一个通过函数指针来调用的函数。</p>
<p>简单来讲：回调函数是由别人的函数执行时调用你传入的函数（通过函数指针完成）</p>
<p>人话：<strong>在一个函数里调用另一个函数，被调用的函数是通过参数传入的，这个被调用的函数就叫做回调函数</strong></p>
</blockquote>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cstdlib&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cstdio&gt;</span></span><br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 初始化一个数组</span><br><span class="hljs-comment"> * @param array 数组的指针 </span><br><span class="hljs-comment"> * @param arraySize 数组长度</span><br><span class="hljs-comment"> * @param fun 回调函数</span><br><span class="hljs-comment"> */</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">initArray</span><span class="hljs-params">(<span class="hljs-keyword">int</span> *array, <span class="hljs-keyword">int</span> arraySize, <span class="hljs-keyword">int</span> (*fun)(<span class="hljs-keyword">void</span>))</span> </span>&#123;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; arraySize; ++i) &#123;<br>        <span class="hljs-comment">// 循环调用回调函数，为数组赋随机值</span><br>        array[i] = <span class="hljs-built_in">fun</span>();<br>    &#125;<br>&#125;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 获取随机值</span><br><span class="hljs-comment"> * @return 生成的随机值</span><br><span class="hljs-comment"> */</span><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">getRandNum</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-keyword">return</span> <span class="hljs-built_in">rand</span>();<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-keyword">int</span> array[<span class="hljs-number">20</span>];<br>    <span class="hljs-keyword">int</span> arraySize = <span class="hljs-number">20</span>;<br>    <span class="hljs-comment">// 调用初始化方法，传入回调函数</span><br>    <span class="hljs-built_in">initArray</span>(array, arraySize, getRandNum);<br>    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; arraySize; ++i) &#123;<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d\n&quot;</span>, array[i]);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="文件操作"><a href="#文件操作" class="headerlink" title="文件操作"></a>文件操作</h2><h3 id="打开文件"><a href="#打开文件" class="headerlink" title="打开文件"></a>打开文件</h3><p>使用 <code>fopen()</code> 函数来创建一个新的文件或者打开一个已有的文件，这个调用会初始化类型 <code>FILE</code> 的一个对象，类型 FILE 包含了所有用来控制流的必要的信息。下面是这个函数调用的原型：</p>
<p><code>FILE *fopen( const char * filename, const char * mode );</code></p>
<ol>
<li><p>说明：在这里，filename 是字符串，用来命名文件，访问模式 mode 的值可以是下列值中的一个</p>
</li>
<li><p>如果处理的是二进制文件(图片，视频..)，则需使用下面的访问模式: “rb”, “wb”, “ab”, “rb+”, “r+b”, “wb+”, “w+b”, “ab+”, “a+b” //b :binary 二进制</p>
</li>
<li><p>打开模式说明</p>
<p><img src="https://yechen-blog-image.oss-cn-hangzhou.aliyuncs.com/images/image-20230228150047109.png" srcset="/img/loading.gif" lazyload alt="image-20230228150047109"></p>
</li>
</ol>
<h3 id="读取文件"><a href="#读取文件" class="headerlink" title="读取文件"></a>读取文件</h3><p>下面是从文件读取<strong>单个字符</strong>的函数 <code>int fgetc( FILE * fp );</code></p>
<ul>
<li><strong>说明：</strong>fgetc() 函数从 fp 所指向的输入文件中读取一个字符。返回值是读取的字符，如果发生错误则返回 EOF。</li>
</ul>
<p>下面的函数从流中读取<strong>一个字符串</strong>：<code>char *fgets( char *buf, int n, FILE *fp );</code></p>
<ul>
<li><strong>说明：</strong>函数 fgets() 从 fp 所指向的输入流中读取 n - 1 个字符。它会把读取的字符串复制到缓冲区 buf，并在最后追加一个 null 字符来终止字符串。</li>
<li>如果这个函数在读取最后一个字符之前就遇到一个换行符 ‘\n’ 或文件的末尾 EOF，则只会返回读取到的字符，包括换行符。</li>
<li>也可以使用 <code>int fscanf(FILE *fp, const char *format, ...)</code> 函数来从文件中读取字符串，但是在遇到第一个空格字符时，它会停止读取。</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cstdio&gt;</span></span><br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br>    FILE *fp = <span class="hljs-literal">nullptr</span>;<br>    <span class="hljs-comment">// 打开文件</span><br>    fp = <span class="hljs-built_in">fopen</span>(<span class="hljs-string">&quot;D:\\Desktop\\test.txt&quot;</span>, <span class="hljs-string">&quot;r&quot;</span>);<br>    <span class="hljs-comment">// 读取单个字符</span><br>    <span class="hljs-keyword">char</span> x = <span class="hljs-built_in">fgetc</span>(fp);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%c\n&quot;</span>, x);<br><br>    <span class="hljs-comment">// 读取一个字符串</span><br>    <span class="hljs-comment">// 定义一个缓冲区</span><br>    <span class="hljs-keyword">char</span> buffer1[<span class="hljs-number">1024</span>];<br>    <span class="hljs-built_in">fgets</span>(buffer1, <span class="hljs-number">1024</span>, fp);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%s\n&quot;</span>, buffer1);<br><br>    <span class="hljs-comment">// 读取整个文件</span><br>    <span class="hljs-keyword">char</span> buffer2[<span class="hljs-number">1024</span>];<br>    <span class="hljs-keyword">while</span> (<span class="hljs-built_in">fgets</span>(buffer2, <span class="hljs-number">1024</span>, fp) != <span class="hljs-literal">nullptr</span>) &#123;<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%s&quot;</span>, buffer2);<br>    &#125;<br>    <span class="hljs-built_in">fclose</span>(fp);<br><br>&#125;   <br></code></pre></td></tr></table></figure>

<h3 id="写入文件"><a href="#写入文件" class="headerlink" title="写入文件"></a>写入文件</h3><ul>
<li>下面是把字符写入到流中的函数 <code>int fputc( int c, FILE *fp );</code><ol>
<li><strong>说明：</strong>函数 fputc() 把参数 c 的字符值写入到 fp 所指向的输出流中。</li>
<li>如果写入成功，它会返回写入的字符，如果发生错误，则会返回 EOF。</li>
</ol>
</li>
<li>下面是把字符串写入到流中的函数 <code>int fputs( const char *s, FILE *fp );</code><ol>
<li><strong>说明：</strong>函数 fputs() 把字符串 s 写入到 fp 所指向的输出流中。</li>
<li>如果写入成功，它会返回一个非负值，如果发生错误，则会返回 EOF。</li>
<li>也可以使用 <code>int fprintf(FILE *fp,const char *format, ...)</code> 函数来写把一个字符串写入到文件中</li>
</ol>
</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cstdio&gt;</span></span><br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>&#123;<br>    FILE *fp = <span class="hljs-literal">nullptr</span>;<br>    <span class="hljs-comment">// 打开文件</span><br>    fp = <span class="hljs-built_in">fopen</span>(<span class="hljs-string">&quot;D:\\Desktop\\test.txt&quot;</span>, <span class="hljs-string">&quot;a+&quot;</span>);<br>    <span class="hljs-comment">// 两种写入字符串的方式</span><br>    <span class="hljs-built_in">fputs</span>(<span class="hljs-string">&quot;Hello World!!!!! fputs\n&quot;</span>, fp);<br>    <span class="hljs-built_in">fprintf</span>(fp, <span class="hljs-string">&quot;Hello World!!!!! fprintf\n&quot;</span>);<br>    <span class="hljs-comment">// 关闭文件</span><br>    <span class="hljs-built_in">fclose</span>(fp);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Finish!&quot;</span>);<br><br>&#125;   <br></code></pre></td></tr></table></figure>

<h3 id="关闭文件"><a href="#关闭文件" class="headerlink" title="关闭文件"></a>关闭文件</h3><p>关闭文件，使用 <code>fclose()</code> 函数。函数的原型如下：</p>
<p><code>int fclose( FILE *fp );</code></p>
<ol>
<li>如果<strong>成功关闭</strong>文件，fclose() 函数返回零，如果关闭文件时<strong>发生错误</strong>，函数返回 EOF。这个函数实际上，会清空缓冲区中的数据，关闭文件，并释放用于该文件的所有内存。EOF 是一个定义在头文件 stdio.h 中的常量。</li>
<li>C 标准库提供了各种函数来按字符或者以固定长度字符串的形式读写文件。</li>
<li><strong>使用完文件后(读，写)，一定要将该文件关闭</strong></li>
</ol>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E8%80%83%E7%A0%94/">考研</a>
                    
                      <a class="hover-with-bg" href="/categories/%E8%80%83%E7%A0%94/%E5%A4%8D%E8%AF%95/">复试</a>
                    
                      <a class="hover-with-bg" href="/categories/%E8%80%83%E7%A0%94/%E5%A4%8D%E8%AF%95/C%E8%AF%AD%E8%A8%80/">C语言</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/%E8%80%83%E7%A0%94/">考研</a>
                    
                      <a class="hover-with-bg" href="/tags/%E5%A4%8D%E8%AF%95/">复试</a>
                    
                      <a class="hover-with-bg" href="/tags/C%E8%AF%AD%E8%A8%80/">C语言</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2023/03/06/%E5%A4%8D%E8%AF%95-%E5%A4%8D%E8%AF%95-C%E8%AF%AD%E8%A8%80%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E9%9D%A2%E8%AF%95%E9%97%AE%E7%AD%9401/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">复试-C语言程序设计面试问答01</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/10/30/SSM%E6%A1%86%E6%9E%B6-2021-10-30-MyBatis%E5%AD%A6%E4%B9%A0/">
                        <span class="hidden-mobile">MyBatis学习</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.3/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.1/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js" ></script>



  <script  src="/js/local-search.js" ></script>






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.12/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>














  
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/xiaoxuehua.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/caidai.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/love.js"></script>
<script src="//cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script>
<script src="//cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script>



<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>

  <!--动态线条背景-->
  <script type="text/javascript" color="70,119,184" opacity='1' zIndex="-2" count="200" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"jsonPath":"/live2dw/assets/koharu.model.json"},"display":{"position":"left","width":200,"height":400},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
